/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
 var levelOrder = function(root) {
    if(!root){
        return [];
    }
    let floor = [];
    let res = [];
    let isreserve = false;
    floor.push(root);
    while(floor.length){
        let arr = [];
        let lastfloor=[];
        for(let i =0;i<floor.length;i++){
            if(floor[i].left) lastfloor.push(floor[i].left)
            if(floor[i].right) lastfloor.push(floor[i].right)
            arr.push(floor[i].val)
        }
        if(isreserve) res.push(arr.reverse());
        else res.push(arr);
        isreserve = !isreserve;
        floor=lastfloor;
    }
    return res;
};